import { http } from "@/utils/http";

// 文件/文件夹类型定义
export interface FileItem {
  id: number;
  name: string;
  type: "folder" | "file";
  mime_type: string;
  size: number;
  oss_url: string | null;
  parentId: number | null;
  owner_id: number;
  sort_index: number;
  created_at: string;
  updated_at: string;
  path: string;
  level: number;
  icon_url?: string;
  children?: FileItem[];
}

// 获取文件树参数
export interface GetFileTreeParams {
  keyword?: string; // 搜索关键字
}

// 获取文件列表参数
export interface GetFileListParams {
  parentId?: string; // 父文件夹ID，不传表示根目录
  keyword?: string; // 搜索关键字
}

// 创建文件夹参数
export interface CreateFolderParams {
  name: string;
  parentId?: number;
}

// 重命名参数
export interface RenameParams {
  id: number;
  name: string;
}

// 排序参数
export interface SortParams {
  id: string;
  sort: number;
}

// 批量排序参数
export interface BatchSortParams {
  items: SortParams[];
}

// 批量删除参数
export interface BatchDeleteParams {
  ids: number[];
}

interface SortItemsParams {
  parentId?: number;
  items: {
    id: number;
    sortIndex: number;
  }[];
}

// 上传文件到OSS的返回类型
export interface UploadToOSSResult {
  success: boolean;
  data: {
    url: string; // OSS URL
  };
}

// 更新图标参数
export interface UpdateIconParams {
  id: number;
  iconUrl: string; // OSS URL
}

/**
 * 获取文件树
 */
export const getFileTree = () => {
  return http.request<any>("get", "/api/files/tree");
};

/**
 * 创建文件夹
 */
export const createFolder = (data: CreateFolderParams) => {
  // 确保参数格式正确
  const params = {
    name: data.name,
    parentId: data.parentId // 后端接收的是 parentId
  };

  return http.request<{ success: boolean; data: FileItem }>(
    "post",
    "/api/folders",
    { data: params }
  );
};

/**
 * 重命名文件/文件夹
 */
export const renameItem = (data: RenameParams) => {
  // 确保参数格式正确
  const params = {
    id: data.id,
    name: data.name
  };

  return http.request<{ success: boolean; data: FileItem }>(
    "put",
    "/api/files/rename",
    { data: params }
  );
};

/**
 * 删除文件/文件夹
 */
export const deleteItems = (data: BatchDeleteParams) => {
  return http.request<{ success: boolean; data: null }>(
    "post",
    "/api/files/batch-delete",
    { data }
  );
};

/**
 * 批量排序
 */
export const batchSort = (data: BatchSortParams) => {
  return http.request<any>("put", "/file/sort", { data });
};

/**
 * 上传文件
 */
export const uploadFile = (data: FormData) => {
  return http.request<{ success: boolean; data: FileItem[] }>(
    "post",
    "/api/files/upload",
    {
      data,
      headers: {
        "Content-Type": "multipart/form-data"
      },
      timeout: 60000 // 设置为 60 秒
    }
  );
};

/**
 * 下载文件
 */
export const downloadFile = (id: string) => {
  return http.request<Blob>("get", `/file/download/${id}`, {
    responseType: "blob"
  });
};

/**
 * 获取文件预览链接
 */
export const getPreviewUrl = (id: string) => {
  return http.request<{ url: string }>("get", `/file/preview/${id}`);
};

/**
 * 排序文件/文件夹
 */
export const sortItems = (params: SortItemsParams) => {
  return http.request<{ success: boolean }>("put", "/api/files/sort", {
    data: params
  });
};

/**
 * 上传文件到OSS
 */
export const uploadToOSS = (file: FormData) => {
  return http.request<UploadToOSSResult>("post", "/api/oss/upload", {
    data: file,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/**
 * 更新文件/文件夹图标
 */
export const updateIcon = (data: UpdateIconParams) => {
  return http.request<{ success: boolean; data: FileItem }>(
    "patch",
    "/api/files/icon",
    { data }
  );
};
